package com.lenovo.manufacture.module;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.beardedhen.androidbootstrap.BootstrapLabel;
import com.lenovo.manufacture.Adapter.recAdapter;
import com.lenovo.manufacture.R;
import com.lenovo.manufacture.net.BaseActivity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import butterknife.BindView;
import butterknife.ButterKnife;

public class module8 extends BaseActivity {

    @BindView(R.id.m8_tv)
    BootstrapLabel m8Tv;
    @BindView(R.id.m8_rv)
    RecyclerView m8Rv;
    List<Map<String, String>> cars;
    List<Map<String, String>> cartypes;
    List<Map<String, String>> line = new ArrayList<>();
    int i = 0;
    String s;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_module8);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        async(data.FactoryInfo(t.test("id", 1).b()), v -> {
            s = v.getData().get(0).get("capacity2");
            new Thread(() -> {
                //async
                for (int i1 = 1; i1 < 5; i1++) {
                    try {
                        Map<String, String> id = data.getLine(t.test("id", i1).b()).execute().body().getData().get(0);
                        System.out.println(id);
                        line.add(id);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                async(data.getAllcar(), v3 -> async(data.getcartype(), v1 -> {
                            cars = v3.getData();
                            cartypes = v1.getData();
                            initrec();
                        })
                );
            }).start();
        });

/////////////////////////////

    }

    @SuppressLint("NewApi")
    private void initrec() {
        List<String> list = new ArrayList<>();
        cars.forEach(car -> {
            Map<String, String> carTypeId = cartypes.get(Integer.valueOf(car.get("carTypeId")));
            list.add(car.get("id"));
            list.add(carTypeId.get("carTypeName"));
            list.add(car.get("num"));
            list.add(carTypeId.get("size"));
            i += Integer.valueOf(carTypeId.get("size")) * Integer.valueOf(car.get("num"));
            list.add(line.get(Integer.valueOf(car.get("userLineId")) - 1).get("userLineName"));
            list.add(car.get("type").equals("0") ? "正常" : "维修");
        });
        m8Tv.setText(String.format("成品车辆存库信息：共占地%d,库存容量为%s", i, s));
        m8Rv.setLayoutManager(new GridLayoutManager(this, 6));
        m8Rv.setAdapter(new recAdapter(list));
    }
}